package com.cs.study.homework._0307;

import java.io.*;

public class Demo {
    public static void main(String[] args) {
        MyRunnable myRunnable = new MyRunnable(new File("E:\\a\\a.txt"), new File("E:\\a\\b.txt"));
        Thread t = new Thread(myRunnable);
        t.start();
    }
}

class MyRunnable implements Runnable {

    File oldFile;
    File copyFile;

    public MyRunnable(File oldFile, File copyFile) {
        this.oldFile = oldFile;
        this.copyFile = copyFile;
    }

    @Override
    public void run() {
        try (FileOutputStream out = new FileOutputStream(copyFile);
             FileInputStream in = new FileInputStream(oldFile)) {
            long length = oldFile.length();
            byte[] bytes = new byte[100];
            int readCount;

            double temp = 0;

            while ((readCount = in.read(bytes)) != -1) {
                out.write(bytes);
                temp += readCount;
                double percent = temp / length * 100;
                System.out.println("文件已传输" + String.format("%.2f", percent) + "%");
            }
        } catch (IOException e) {
            try {
                throw new IOException();
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        }
    }
}